<template>
  <div class="app-container">
    <el-row>
      <el-col>
<!--        搜索表单-->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true"  label-width="88px">
          <el-form-item label="平台名称" prop="keyword">
            <el-input v-model="queryParams.keyword" placeholder="请输入平台名称"  @keyup.enter.native="handleQuery"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
<!--        顶部的新增，修改，删除按钮-->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['datasourceBase:add']">注册</el-button>
          </el-col>
<!--          <el-col :span="1.5">-->
<!--            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['datasourceBase:edit']">修改</el-button>-->
<!--          </el-col>-->
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['datasourceBase:remove']">删除</el-button>
          </el-col>
          <right-toolbar  @queryTable="getList"></right-toolbar>
        </el-row>
<!--        表格-->
        <el-table v-loading="loading" :data="platformList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" width="50" align="center" label="序号"></el-table-column>
          <el-table-column label="平台名称" align="center" prop="systemName" width="180" />
          <el-table-column label="所属单位" align="center" prop="unit" width="180" />
          <el-table-column label="所属部门" align="center" prop="depart" :show-overflow-tooltip="true" />
          <el-table-column label="数据库数量" align="center" prop="dbNumber"  />
          <el-table-column label="简介" align="center" prop="detail" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['datasourceBase:edit']" >修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['datasourceBase:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="platformTotal>0" :total="platformTotal" :page.sync="queryParams.currPage" :limit.sync="queryParams.pageSize" @pagination="getList" />
<!--    新增或修改平台弹窗-->
        <el-dialog :title="UpdateTitle" :visible.sync="UpdateOpen" width="50%" append-to-body>
          <el-form :model="platformForm" label-width="120px" :rules="platformRule" ref="platformForm">
            <el-form-item label="平台名称" prop="systemName" required>
              <el-input placeholder="请输入平台名称" v-model="platformForm.systemName"> </el-input>
            </el-form-item>
            <el-form-item label="所属单位" prop="unit" required>
              <el-input placeholder="请输入单位名称" v-model="platformForm.unit"> </el-input>
            </el-form-item>
            <el-form-item label="所属部门" prop="depart" >
              <el-input placeholder="请输入部门名称" v-model="platformForm.depart"> </el-input>
            </el-form-item>
            <el-form-item label="数据库数量" prop="depart" >
              <el-input placeholder="数据库数量" v-model="platformForm.dbNumber" disabled> </el-input>
            </el-form-item>
            <el-form-item label="简介" prop="detail" >
              <el-input v-model="platformForm.detail" placeholder="请输入平台详细介绍" type="textarea" maxlength="1000" show-word-limit />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer" prop="status">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="reset">重置</el-button>
            <el-button @click="cancel">关闭</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>
<script>

import { platformAdd, platformDel, platformEdit, platformList } from '@/api/qysj/app/rawData/platformSystem'

export default {
  name:'PlatformSystem',
  data(){
    return{
      // 查询参数
      queryParams:{
        keyword:null,
        pageSize:10,
        currPage:1
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 加载表格
      loading:true,
      // 平台列表
      platformList:[],
      // 平台总数
      platformTotal:0,
      // 新增表单标题
      UpdateTitle:'',
      // 弹出框状态
      UpdateOpen:false,
      // 平台信息表单
      platformForm:{},
      // 表单验证
      platformRule:{
        systemName:[
          {required: true, message: '请输入平台名称', trigger: 'blur'}
        ],
        unit:[
          {required: true, message: '请输入单位名称', trigger: 'blur'}
        ],
        // detail:[
        //   {required: true, message: '请输入详细介绍', trigger: 'blur'}
        // ]
      },
      selIds:[]
    }


  },
  created() {
    this.getList()
    this.startTimer()
  },
  methods: {
    // 开始定时器
    startTimer() {
      this.timerId = setInterval(() => {
        // 在这里执行定时要执行的操作，例如重新加载数据
        this.getList();
      }, 3*60*1000); //三分钟定时刷新一次
    },
    // 停止定时器
    stopTimer() {
      clearInterval(this.timerId);
    },
    // 获取列表
    getList(){
      platformList(this.queryParams).then(res=>{
        if (res.code===200){
          this.loading=false;
          this.platformList=res.rows;
          this.platformTotal=res.total;
        }else {
          this.$message(res.msg)
        }
      })
    },
    // 多选框处理
    handleSelectionChange(selection){
      this.selIds = selection.map((item) => item.id);
      // console.log('selItems', this.selIds,this.selIds.join(','))
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 搜索按钮
    handleQuery(){
      this.queryParams.pageNum=1;
      this.getList()
    },
    // 重置按钮
    resetQuery(){
      this.resetForm("queryForm");//全局方法
      this.handleQuery();
    },
    //新增按钮
    handleAdd(){
      this.UpdateOpen=true;
      this.UpdateTitle="新增平台信息"
    },
    // 修改按钮
    handleUpdate(data){
      this.UpdateOpen=true;
      this.UpdateTitle="修改平台信息"
      this.platformForm=data;
    },
    // 删除按钮
    handleDelete(data){
      const ids=data.id||this.selIds.join(',')
      platformDel(ids).then(res=>{
        if (res.code===200){
          this.$message('删除成功')
          this.getList()
        }else {
          this.$message(res.msg)
        }
      })
    },
    // 弹出框提交按钮
    submitForm(){
      this.$refs["platformForm"].validate((valid)=>{
        if (valid){
          // console.log('this.platformForm.id', this.platformForm.id)
          if (this.platformForm.id){
           platformEdit(JSON.stringify(this.platformForm)).then(res=>{
             if (res.code===200){
               this.$message('修改成功')
               this.$refs["platformForm"].resetFields();
               this.getList()
               this.UpdateOpen=false;
             }else {
               this.$message(res.msg)
             }
           })
         }else {
           platformAdd(JSON.stringify(this.platformForm)).then(res=>{
             if (res.code===200){
               this.$message('新增成功')
               this.$refs["platformForm"].resetFields()
               this.getList()
               this.UpdateOpen=false;
             }else {
               this.$message(res.msg)
             }
           })
         }
        }
      })
    },
    // 弹出框取消
    cancel(){
     this.UpdateOpen=false;
    },
    reset(){
      this.platformForm={}
    }
  },
  beforeDestroy() {
    // 在组件销毁前，停止定时器以防止内存泄漏
    this.stopTimer();
  },
}

</script>
